/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.annotations.VisibleForTesting;
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Flag
/*     */   extends Tag
/*     */ {
/*     */   private static final char HIDDEN = '\000';
/*  36 */   private static final FlagInfo[] INDEX2FLAG = new FlagInfo[31];
/*  37 */   private static final FlagInfo[] CHAR2FLAG = new FlagInfo[127];
/*  38 */   private static final Map<String, FlagInfo> NAME2FLAG = new HashMap();
/*     */   
/*     */   public static enum FlagInfo {
/*  41 */     FROM_ME(-1, "\\Sent", 's'), 
/*  42 */     ATTACHED(-2, "\\Attached", 'a'), 
/*  43 */     REPLIED(-3, "\\Answered", 'r'), 
/*  44 */     FORWARDED(-4, "\\Forwarded", 'w'), 
/*  45 */     COPIED(-5, "\\Copied", '\000'), 
/*  46 */     FLAGGED(-6, "\\Flagged", 'f'), 
/*  47 */     DRAFT(-7, "\\Draft", 'd'), 
/*  48 */     DELETED(-8, "\\Deleted", 'x'), 
/*  49 */     NOTIFIED(-9, "\\Notified", 'n'), 
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  55 */     UNREAD(-10, "\\Unread", 'u'), 
/*  56 */     HIGH_PRIORITY(-11, "\\Urgent", '!'), 
/*  57 */     LOW_PRIORITY(-12, "\\Bulk", '?'), 
/*  58 */     VERSIONED(-13, "\\Versioned", '/'), 
/*     */     
/*     */ 
/*     */ 
/*  62 */     INDEXING_DEFERRED(-14, "\\IdxDeferred", '\000'), 
/*     */     
/*  64 */     POPPED(-15, "\\Popped", 'p'), 
/*  65 */     NOTE(-16, "\\Note", 't'), 
/*  66 */     PRIORITY(-17, "\\Priority", '+'), 
/*  67 */     POST(-18, "\\Post", '^'), 
/*  68 */     MUTED(-19, "\\Muted", '('), 
/*  69 */     SUBSCRIBED(-20, "\\Subscribed", '*'), 
/*  70 */     EXCLUDE_FREEBUSY(-21, "\\ExcludeFB", 'b'), 
/*  71 */     CHECKED(-22, "\\Checked", '#'), 
/*  72 */     NO_INHERIT(-23, "\\NoInherit", 'i'), 
/*  73 */     INVITE(-24, "\\Invite", 'v'), 
/*  74 */     SYNCFOLDER(-25, "\\SyncFolder", 'y'), 
/*  75 */     SYNC(-26, "\\Sync", '~'), 
/*  76 */     NO_INFERIORS(-27, "\\Noinferiors", 'o'), 
/*     */     
/*     */ 
/*     */ 
/*  80 */     ARCHIVED(-28, "\\Archived", '@'), 
/*     */     
/*  82 */     GLOBAL(-29, "\\Global", 'g'), 
/*  83 */     IN_DUMPSTER(-30, "\\InDumpster", '\000'), 
/*  84 */     UNCACHED(-31, "\\Uncached", '\000');
/*     */     
/*     */ 
/*     */     final String flagName;
/*     */     final int id;
/*     */     final char ch;
/*     */     final int bitmask;
/*     */     
/*     */     private FlagInfo(int id, String name, char ch)
/*     */     {
/*  94 */       this.id = id;
/*  95 */       this.flagName = name;
/*  96 */       this.ch = ch;
/*  97 */       this.bitmask = (1 << -id - 1);
/*     */       
/*  99 */       Flag.INDEX2FLAG[Flag.getIndex(id)] = this;
/* 100 */       Flag.NAME2FLAG.put(name.toLowerCase(), this);
/* 101 */       if (ch != 0) {
/* 102 */         Flag.CHAR2FLAG[ch] = this;
/*     */       }
/*     */     }
/*     */     
/*     */     Flag toFlag(Mailbox mbox) throws ServiceException {
/* 107 */       MailItem.UnderlyingData data = new MailItem.UnderlyingData();
/* 108 */       data.id = this.id;
/* 109 */       data.type = MailItem.Type.FLAG.toByte();
/* 110 */       data.folderId = 8;
/* 111 */       data.setFlags(Flag.BITMASK_UNCACHED);
/* 112 */       data.name = this.flagName;
/* 113 */       return new Flag(mbox, data, this);
/*     */     }
/*     */     
/*     */     @VisibleForTesting
/*     */     boolean isHidden() {
/* 118 */       return this.ch == 0;
/*     */     }
/*     */     
/*     */     public int toId() {
/* 122 */       return this.id;
/*     */     }
/*     */     
/*     */     public int toBitmask() {
/* 126 */       return this.bitmask;
/*     */     }
/*     */     
/*     */     public String toString()
/*     */     {
/* 131 */       return this.flagName;
/*     */     }
/*     */     
/*     */     static FlagInfo of(String fname) {
/* 135 */       return (FlagInfo)Flag.NAME2FLAG.get(fname.toLowerCase());
/*     */     }
/*     */     
/*     */     static FlagInfo of(int id) {
/* 139 */       int index = Flag.getIndex(id);
/* 140 */       return (index < 0) || (index >= Flag.INDEX2FLAG.length) ? null : Flag.INDEX2FLAG[index];
/*     */     }
/*     */   }
/*     */   
/* 144 */   public static final int ID_FROM_ME = FlagInfo.FROM_ME.id;
/* 145 */   public static final int ID_ATTACHED = FlagInfo.ATTACHED.id;
/* 146 */   public static final int ID_REPLIED = FlagInfo.REPLIED.id;
/* 147 */   public static final int ID_FORWARDED = FlagInfo.FORWARDED.id;
/* 148 */   public static final int ID_COPIED = FlagInfo.COPIED.id;
/* 149 */   public static final int ID_FLAGGED = FlagInfo.FLAGGED.id;
/* 150 */   public static final int ID_DRAFT = FlagInfo.DRAFT.id;
/* 151 */   public static final int ID_DELETED = FlagInfo.DELETED.id;
/* 152 */   public static final int ID_NOTIFIED = FlagInfo.NOTIFIED.id;
/* 153 */   public static final int ID_UNREAD = FlagInfo.UNREAD.id;
/* 154 */   public static final int ID_HIGH_PRIORITY = FlagInfo.HIGH_PRIORITY.id;
/* 155 */   public static final int ID_LOW_PRIORITY = FlagInfo.LOW_PRIORITY.id;
/* 156 */   public static final int ID_VERSIONED = FlagInfo.VERSIONED.id;
/*     */   @Deprecated
/* 158 */   public static final int ID_INDEXING_DEFERRED = FlagInfo.INDEXING_DEFERRED.id;
/* 159 */   public static final int ID_POPPED = FlagInfo.POPPED.id;
/* 160 */   public static final int ID_NOTE = FlagInfo.NOTE.id;
/* 161 */   public static final int ID_PRIORITY = FlagInfo.PRIORITY.id;
/* 162 */   public static final int ID_POST = FlagInfo.POST.id;
/* 163 */   public static final int ID_MUTED = FlagInfo.MUTED.id;
/* 164 */   public static final int ID_SUBSCRIBED = FlagInfo.SUBSCRIBED.id;
/* 165 */   public static final int ID_EXCLUDE_FREEBUSY = FlagInfo.EXCLUDE_FREEBUSY.id;
/* 166 */   public static final int ID_CHECKED = FlagInfo.CHECKED.id;
/* 167 */   public static final int ID_NO_INHERIT = FlagInfo.NO_INHERIT.id;
/* 168 */   public static final int ID_INVITE = FlagInfo.INVITE.id;
/* 169 */   public static final int ID_SYNCFOLDER = FlagInfo.SYNCFOLDER.id;
/* 170 */   public static final int ID_SYNC = FlagInfo.SYNC.id;
/* 171 */   public static final int ID_NO_INFERIORS = FlagInfo.NO_INFERIORS.id;
/*     */   @Deprecated
/* 173 */   public static final int ID_ARCHIVED = FlagInfo.ARCHIVED.id;
/* 174 */   public static final int ID_GLOBAL = FlagInfo.GLOBAL.id;
/* 175 */   public static final int ID_IN_DUMPSTER = FlagInfo.IN_DUMPSTER.id;
/* 176 */   public static final int ID_UNCACHED = FlagInfo.UNCACHED.id;
/*     */   
/* 178 */   public static final int BITMASK_FROM_ME = FlagInfo.FROM_ME.bitmask;
/* 179 */   public static final int BITMASK_ATTACHED = FlagInfo.ATTACHED.bitmask;
/* 180 */   public static final int BITMASK_REPLIED = FlagInfo.REPLIED.bitmask;
/* 181 */   public static final int BITMASK_FORWARDED = FlagInfo.FORWARDED.bitmask;
/* 182 */   public static final int BITMASK_COPIED = FlagInfo.COPIED.bitmask;
/* 183 */   public static final int BITMASK_FLAGGED = FlagInfo.FLAGGED.bitmask;
/* 184 */   public static final int BITMASK_DRAFT = FlagInfo.DRAFT.bitmask;
/* 185 */   public static final int BITMASK_DELETED = FlagInfo.DELETED.bitmask;
/* 186 */   public static final int BITMASK_NOTIFIED = FlagInfo.NOTIFIED.bitmask;
/* 187 */   public static final int BITMASK_UNREAD = FlagInfo.UNREAD.bitmask;
/* 188 */   public static final int BITMASK_HIGH_PRIORITY = FlagInfo.HIGH_PRIORITY.bitmask;
/* 189 */   public static final int BITMASK_LOW_PRIORITY = FlagInfo.LOW_PRIORITY.bitmask;
/* 190 */   public static final int BITMASK_VERSIONED = FlagInfo.VERSIONED.bitmask;
/*     */   @Deprecated
/* 192 */   public static final int BITMASK_INDEXING_DEFERRED = FlagInfo.INDEXING_DEFERRED.bitmask;
/* 193 */   public static final int BITMASK_POPPED = FlagInfo.POPPED.bitmask;
/* 194 */   public static final int BITMASK_NOTE = FlagInfo.NOTE.bitmask;
/* 195 */   public static final int BITMASK_PRIORITY = FlagInfo.PRIORITY.bitmask;
/* 196 */   public static final int BITMASK_POST = FlagInfo.POST.bitmask;
/* 197 */   public static final int BITMASK_MUTED = FlagInfo.MUTED.bitmask;
/* 198 */   public static final int BITMASK_SUBSCRIBED = FlagInfo.SUBSCRIBED.bitmask;
/* 199 */   public static final int BITMASK_EXCLUDE_FREEBUSY = FlagInfo.EXCLUDE_FREEBUSY.bitmask;
/* 200 */   public static final int BITMASK_CHECKED = FlagInfo.CHECKED.bitmask;
/* 201 */   public static final int BITMASK_NO_INHERIT = FlagInfo.NO_INHERIT.bitmask;
/* 202 */   public static final int BITMASK_INVITE = FlagInfo.INVITE.bitmask;
/* 203 */   public static final int BITMASK_SYNCFOLDER = FlagInfo.SYNCFOLDER.bitmask;
/* 204 */   public static final int BITMASK_SYNC = FlagInfo.SYNC.bitmask;
/* 205 */   public static final int BITMASK_NO_INFERIORS = FlagInfo.NO_INFERIORS.bitmask;
/*     */   @Deprecated
/* 207 */   public static final int BITMASK_ARCHIVED = FlagInfo.ARCHIVED.bitmask;
/* 208 */   public static final int BITMASK_GLOBAL = FlagInfo.GLOBAL.bitmask;
/* 209 */   public static final int BITMASK_IN_DUMPSTER = FlagInfo.IN_DUMPSTER.bitmask;
/* 210 */   public static final int BITMASK_UNCACHED = FlagInfo.UNCACHED.bitmask;
/*     */   
/* 212 */   static final String UNREAD_FLAG_ONLY = String.valueOf(FlagInfo.UNREAD.ch);
/*     */   
/* 214 */   public static final int FLAGS_SYSTEM = BITMASK_FROM_ME | BITMASK_ATTACHED | BITMASK_COPIED | BITMASK_DRAFT | BITMASK_HIGH_PRIORITY | BITMASK_LOW_PRIORITY | BITMASK_VERSIONED | BITMASK_INDEXING_DEFERRED | BITMASK_INVITE | BITMASK_ARCHIVED | BITMASK_GLOBAL | BITMASK_IN_DUMPSTER | BITMASK_UNCACHED | BITMASK_NOTE | BITMASK_POST;
/*     */   
/*     */ 
/*     */ 
/* 218 */   public static final int FLAGS_FOLDER = BITMASK_SUBSCRIBED | BITMASK_EXCLUDE_FREEBUSY | BITMASK_CHECKED | BITMASK_NO_INHERIT | BITMASK_SYNCFOLDER | BITMASK_SYNC | BITMASK_NO_INFERIORS | BITMASK_GLOBAL;
/*     */   
/*     */ 
/* 221 */   public static final int FLAGS_MESSAGE = BITMASK_FROM_ME | BITMASK_REPLIED | BITMASK_FORWARDED | BITMASK_DRAFT | BITMASK_NOTIFIED | BITMASK_UNREAD | BITMASK_HIGH_PRIORITY | BITMASK_LOW_PRIORITY | BITMASK_POPPED | BITMASK_INVITE | BITMASK_PRIORITY | BITMASK_POST | BITMASK_MUTED;
/*     */   
/*     */ 
/*     */ 
/* 225 */   public static final int FLAGS_CALITEM = BITMASK_DRAFT | BITMASK_HIGH_PRIORITY | BITMASK_LOW_PRIORITY;
/*     */   
/* 227 */   public static final int FLAGS_GENERIC = BITMASK_ATTACHED | BITMASK_COPIED | BITMASK_FLAGGED | BITMASK_DELETED | BITMASK_VERSIONED | BITMASK_INDEXING_DEFERRED | BITMASK_ARCHIVED | BITMASK_IN_DUMPSTER | BITMASK_UNCACHED | BITMASK_NOTE;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 234 */   public static final int FLAGS_ALL = (FLAGS_FOLDER | FLAGS_MESSAGE | FLAGS_CALITEM | FLAGS_GENERIC) & (BITMASK_UNREAD ^ 0xFFFFFFFF);
/*     */   private final FlagInfo info;
/*     */   
/*     */   Flag(Mailbox mbox, MailItem.UnderlyingData ud, FlagInfo info) throws ServiceException
/*     */   {
/* 239 */     super(mbox, ud);
/* 240 */     if (this.mData.type != MailItem.Type.FLAG.toByte()) {
/* 241 */       throw new IllegalArgumentException();
/*     */     }
/* 243 */     this.info = info;
/*     */   }
/*     */   
/*     */   public static byte getIndex(int flagId) {
/* 247 */     return (byte)((flagId > 0) || (flagId < -31) ? -1 : -flagId - 1);
/*     */   }
/*     */   
/*     */   public char toChar() {
/* 251 */     return toChar(this.mId);
/*     */   }
/*     */   
/*     */   public static char toChar(int flagId) {
/* 255 */     int index = getIndex(flagId);
/* 256 */     if ((index < 0) || (index >= INDEX2FLAG.length)) {
/* 257 */       return '\000';
/*     */     }
/*     */     
/* 260 */     FlagInfo finfo = INDEX2FLAG[index];
/* 261 */     return finfo == null ? '\000' : finfo.ch;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static int toBitmask(String flags)
/*     */   {
/* 268 */     if (Strings.isNullOrEmpty(flags)) {
/* 269 */       return 0;
/*     */     }
/*     */     
/* 272 */     int bitmask = 0;
/* 273 */     int i = 0; for (int len = flags.length(); i < len; i++) {
/* 274 */       char c = flags.charAt(i);
/* 275 */       FlagInfo flag = (c > 0) && (c < '') ? CHAR2FLAG[c] : null;
/* 276 */       if (flag != null) {
/* 277 */         bitmask |= flag.bitmask;
/*     */       }
/*     */     }
/* 280 */     return bitmask;
/*     */   }
/*     */   
/*     */   public static String toString(int bitmask) {
/* 284 */     if (bitmask == 0) {
/* 285 */       return "";
/*     */     }
/*     */     
/* 288 */     StringBuilder result = new StringBuilder();
/* 289 */     for (FlagInfo flag : FlagInfo.values()) {
/* 290 */       if (((bitmask & flag.bitmask) != 0) && 
/* 291 */         (flag.ch != 0)) {
/* 292 */         result.append(flag.ch);
/*     */       }
/*     */     }
/*     */     
/* 296 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static List<Integer> toId(int bitmask) {
/* 300 */     if (bitmask == 0) {
/* 301 */       return Collections.emptyList();
/*     */     }
/*     */     
/* 304 */     List<Integer> result = new ArrayList(5);
/* 305 */     for (FlagInfo flag : FlagInfo.values()) {
/* 306 */       if ((bitmask & flag.bitmask) != 0) {
/* 307 */         result.add(Integer.valueOf(flag.id));
/*     */       }
/*     */     }
/* 310 */     return result;
/*     */   }
/*     */   
/*     */ 
/*     */   static Flag of(Mailbox mbox, int id)
/*     */     throws ServiceException
/*     */   {
/* 317 */     int index = getIndex(id);
/* 318 */     FlagInfo flag = (index < 0) || (index >= INDEX2FLAG.length) ? null : INDEX2FLAG[index];
/* 319 */     return flag == null ? null : flag.toFlag(mbox);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static Flag of(Mailbox mbox, String name)
/*     */     throws ServiceException
/*     */   {
/* 329 */     FlagInfo flag = (FlagInfo)NAME2FLAG.get(name.toLowerCase());
/* 330 */     return flag == null ? null : flag.toFlag(mbox);
/*     */   }
/*     */   
/*     */ 
/*     */   static List<Flag> allOf(Mailbox mbox)
/*     */     throws ServiceException
/*     */   {
/* 337 */     FlagInfo[] flags = FlagInfo.values();
/* 338 */     List<Flag> result = new ArrayList(flags.length);
/* 339 */     for (FlagInfo flag : flags) {
/* 340 */       result.add(flag.toFlag(mbox));
/*     */     }
/* 342 */     return result;
/*     */   }
/*     */   
/*     */   public byte getIndex() {
/* 346 */     return getIndex(this.mId);
/*     */   }
/*     */   
/*     */   boolean canTag(MailItem item)
/*     */   {
/* 351 */     if (((this.info.bitmask & FLAGS_FOLDER) != 0) && ((item instanceof Folder))) {
/* 352 */       return true;
/*     */     }
/* 354 */     if (!item.isTaggable()) {
/* 355 */       return false;
/*     */     }
/* 357 */     if (((this.info.bitmask & FLAGS_MESSAGE) != 0) && (!(item instanceof Message))) {
/* 358 */       return false;
/*     */     }
/* 360 */     if (((this.info.bitmask & FLAGS_CALITEM) != 0) && (!(item instanceof CalendarItem))) {
/* 361 */       return false;
/*     */     }
/* 363 */     return true;
/*     */   }
/*     */   
/*     */   boolean canAccess(short rightsNeeded)
/*     */   {
/* 368 */     return true;
/*     */   }
/*     */   
/*     */   boolean canAccess(short rightsNeeded, Account authuser, boolean asAdmin)
/*     */   {
/* 373 */     return true;
/*     */   }
/*     */   
/*     */   boolean isMutable()
/*     */   {
/* 378 */     return false;
/*     */   }
/*     */   
/*     */   boolean trackUnread()
/*     */   {
/* 383 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   void decodeMetadata(Metadata meta) {}
/*     */   
/*     */ 
/*     */   Metadata encodeMetadata(Metadata meta)
/*     */   {
/* 392 */     return meta;
/*     */   }
/*     */   
/*     */   public int toBitmask() {
/* 396 */     return this.info.bitmask;
/*     */   }
/*     */   
/*     */   public boolean isSystemFlag() {
/* 400 */     return (toBitmask() & FLAGS_SYSTEM) != 0;
/*     */   }
/*     */   
/*     */   public boolean equals(Object o)
/*     */   {
/* 405 */     return ((o instanceof Flag)) && (this.info == ((Flag)o).info);
/*     */   }
/*     */   
/*     */   public int hashCode()
/*     */   {
/* 410 */     return this.info.hashCode();
/*     */   }
/*     */   
/*     */   protected void checkItemCreationAllowed()
/*     */     throws ServiceException
/*     */   {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/Flag.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */